<?php
/**
 *
 *
 *
 */
namespace Yz\modules\Generic;

class Action_SelectList
    extends Action_Select
    implements \Yz_Action_Interface
{

    /**
     *  @var int ?
     */
    protected $offset;
    protected $limit;
    protected $calcFoundRows = false;

    /**
     *  @param int $limit >= 0
     *  @return this
     */
    public function setLimit( $limit )
    {
        debug();
        $this->limit = \Yz_Int::testNotNegative($limit);
        $this->calcFoundRows = 0 !== $this->limit;
        return $this;
    }

    /**
     *  @param int $offset >= 0
     *  @return this
     */
    public function setOffset( $offset )
    {
        debug();
        $this->offset = \Yz_Int::testNotNegative($offset);
        return $this;
    }

    /**
     *  Modified SELECT statement creator
     *
     */
    protected function buildSelect()
    {
        debug();
        $sql = parent::buildSelect();
        $sql
            ->calcFoundRows($this->calcFoundRows)
            ->limit($this->offset, $this->limit)
            ->orderBy($this->primaryTable . ".id");
        return $sql;
    }


    /**
     *  @return int
     */
    public function getFoundRows()
    {
        debug();
        return $this->getStatement()->getFoundRows();
    }


    /**
     *  @return Yz_Hash
     */
    public function fetch()
    {
        return $this->getStatement()->fetchAll();
    }

}
